//
//  VIZAFNetWorkTools.swift
//  VIZ微博
//
//  Created by 位立志 on 16/4/10.
//  Copyright © 2016年 位立志. All rights reserved.
//

import UIKit
import AFNetworking


//使用结构体来定义结构体
struct API {
    //线上环境
    static let onlineHost = "https://api.weibo.com/"
    //测试环境  在上次AppStore之前 一定要取消使用这个环境
    static let devHost = "https://dev.api.weibo.com/"
    static let authrizon = "oauth2/access_token"
    static let access_token = "2/users/show.json"
    static let homePage = "2/statuses/home_timeline.json"
}


//swift中枚举: 用. 来调用
enum HTTPMethod : String {
    
    case POST = "POST"
    case GET = "GET"
    
}



class VIZAFNetWorkTools: AFHTTPSessionManager {
    
    
    //声明单例对象 懒加载
    static let shardTools : VIZAFNetWorkTools = {
        let tool = VIZAFNetWorkTools(baseURL: NSURL(string: API.onlineHost))
        tool.responseSerializer.acceptableContentTypes?.insert("text/plain")
        return tool
        
    }()
    
}

//网络请求的核心方法: 所有的get和post都从这里调用
extension VIZAFNetWorkTools{
    
    //封装网络请求方法
    func request(method: HTTPMethod, urlString:String, parameters:AnyObject, finished:(result:AnyObject?, error:NSError?)->()){
        
        if method == .GET {
            self.GET(urlString, parameters: parameters, progress: nil, success: { (task:NSURLSessionDataTask, result:AnyObject?) -> Void in
                finished(result: result!, error: nil)
                
                }, failure: { (task:NSURLSessionDataTask?, error:NSError) -> Void in
                    
                    finished(result: nil, error: error)
            })
        }else{
            self.POST(urlString, parameters: parameters, progress: nil, success: { (task:NSURLSessionDataTask, result:AnyObject?) -> Void in
                finished(result: result!, error: nil)
                
                }, failure: { (task:NSURLSessionDataTask?, error:NSError) -> Void in
                    
                    finished(result: nil, error: error)
                    
            })
            
        }
    }

    
}



//登录和注册相关的网络请求
extension VIZAFNetWorkTools{
    
    //获取token
    func loadAccessToken(code:String, finished:(result:AnyObject?, error:NSError?)->()){
        //let urlString = "https://api.weibo.com/oauth2/access_token"
        //let urlString = API.onlineHost+API.access_token
        let parameters = ["client_id":client_id,"client_secret":client_secret,"grant_type":"authorization_code","code":code,"redirect_uri":redirect_uri]
        
        request(.POST, urlString: API.authrizon, parameters: parameters, finished: finished)
    }
    
    
    //获取用户信息
    func getUserInfo(token:String, uid:AnyObject, finished:(result:AnyObject?, error:NSError?)->()) {
        
        //let url = "https://api.weibo.com/2/users/show.json"

        let parameters = ["access_token":token,"uid":uid]
        
        request(.GET, urlString: API.access_token, parameters: parameters, finished: finished)
    
    }

}


//首页相关的网络请求
extension VIZAFNetWorkTools{
    
    func loadHomePageData(token:String, finished:(result:AnyObject?, error:NSError?)->()){
        print(token)
        let parameters = ["access_token":token]
        request(.GET, urlString: API.homePage, parameters: parameters, finished: finished)
        
    }
    
    
}













